//page里传递的是一个object对象
Page({
  //这里是定义数据的，可以理解为定义的变量，或者方程式中的未知数
  data: {
    //属性的定义 语法为 名称：值
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    circular: true,
    duration: 500,
    height: 270,
    pageSize:4,
    currentPage:1,
    totalPage:0,
    slideType: "default",
    bindanimationfinish: "linear",
    navState: 0, //导航状态
    navName:"",//新闻类别名字
    banners: [],//顶部导航栏图片
    poslist:[],//中间三个广告
    newsCategory:[],//首页新闻类别
    newsList:[]//首页新闻列表
  },
  //监听滑块
  bindchange(e) {
    let index = e.detail.current;
    this.setData({
      navState: index
    });
  },
  //点击导航
  navSwitch: function (e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      navState: index,
      navName:this.data.newsCategory[index].dictName
    });
    this.queryNewsList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.queryBanner();
    this.queryNewsCategory();
    this.queryposlist();
    this.queryNewsList();
  },
  //头部滑动导航栏图片
  queryBanner: function () {
    let _this = this;
    //轮播广告接口地址：http://202.58.105.166/cf/business/banner/wxlist
    wx.request({
      url: 'http://202.58.105.166/cf/business/banner/wxlist',
      method: 'GET',
      dataType: 'json',
      success: function name(res) {
        if (res.statusCode == 200) {
          let result = res.data.data;
          _this.setData({
            //给banners赋值
            banners: result
          })
        }
      }
    })
  },
  //首页中间三个广告
  queryposlist:function(){
    let _this=this;
    //新闻类别接口地址：http://202.58.105.166/cf/sys/dict/item?type=NewsCategory
    wx.request({
      url: 'http://202.58.105.166/cf/business/banner/poslist',
      method:'GET',
      success:function getNewsCate(res){
        if(res.statusCode==200){
          let result=res.data.data;
          _this.setData({
            poslist:result
          })
        }
      }
    });
  },
  //底部新闻类别查询
  queryNewsCategory:function(){
    let _this=this;
    //新闻类别接口地址：http://202.58.105.166/cf/sys/dict/item?type=NewsCategory
    wx.request({
      url: 'http://202.58.105.166/cf/sys/dict/item?type',
      method:'GET',
      data:{
        type: 'NewsCategory'
      },
      success:function getNewsCate(res){
        if(res.statusCode==200){
          let result=res.data.data;
          _this.setData({
            newsCategory:result
          })
        }
      }
    });
  },
  //新闻列表查询
  queryNewsList:function(){
    let _this=this;
    wx.request({
      url: 'http://202.58.105.166/cf/news/wx/list',
      method:'POST',
      data:{
          page: _this.data.currentPage,
          pageSize: _this.data.pageSize,
          category: _this.data.navName
      },
      success:function getNewsCate(res){
        let result=res.data.data;
        if(res.statusCode==200){
          if(res.data.page==1){
            _this.setData({
              newsList:result
            })
          }else{
            result=_this.data.newsList.concat(result);
            _this.setData({
              newsList:result
            })
          }
          _this.data.totalPage=res.data.pageSize;
          _this.data.currentPage=res.data.page;
          //获取底部文章的最大高度
          let size=_this.data.newsList.length*270;
          _this.setData({
            height:size,
          })
        }
      }
    });
  },
  //下拉页面
  onPullDownRefresh: function() {
    this.data.currentPage=1;
    this.queryNewsList();
  },
  // 页面触底时执行更新新闻信息
  onReachBottom: function() {
    if(this.data.currentPage<this.data.totalPage){
      this.data.currentPage++;
      this.queryNewsList();
    }else{
      wx.showToast({
        title: '到底啦...',
        icon: 'none',
        mask:true
    })
    }
  },
  //跳转到洗车页面
  navWash:function(){
    wx.navigateTo({
      url: '/pages/wash/washSite'
    })
  },
  //跳转到加油页面
  navOil:function(){
    wx.switchTab({
      url: '/pages/oil/oilsite'
    })
  },
  //跳转到商城页面
  navShop:function(){
    wx.switchTab({
      url: '/pages/shop/shop'
    })
  },
  //跳转到新闻详情页
  goToContent:function(e){
    wx.navigateTo({
      url: '/pages/index/content/content?id='+e.currentTarget.dataset.id,
    })
  }
})